/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mattespill.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import mattespill.Bruker;
import mattespill.Oversikt;

/**
 *
 * @author Axels
 */
public class GUISlettBruker extends JPanel{

    ListePanel lp;
    Oversikt o;
    private Color c             = new Color(255,255,255,0);

    public GUISlettBruker(Oversikt o) {
        this.setBackground(c);
        this.o = o;
        lp  = new ListePanel(o);
        add(lp);
        setLayout(new GridLayout(1,1));
        }
    }

    
    
    

class ListePanel extends JPanel{

    ArrayList<Bruker> brukere;
    DefaultListModel model = new DefaultListModel();

    JList liste = new JList();
    JScrollPane l;

    JButton slett   = new Knapp("Slett");
    JButton tilbake = new Knapp("Tilbake");
    Knappelytter k = new Knappelytter();
    Oversikt o;
    JPanel kpan = new JPanel();
    private Color c             = new Color(255,255,255, 150);

    public ListePanel(Oversikt o) {
        setLayout(new BorderLayout());
        setBackground(c);
        this.o = o;
        liste.setMinimumSize(new Dimension(100,400));
        //liste.setBackground(c);
        liste.setModel(model);
        brukere = o.getElever(o.getBruker().getBruker_id());
        liste = new JList(brukere.toArray());
        l = new JScrollPane(liste);
        l.setPreferredSize(new Dimension(280, 180));
        l.setBackground(c);
        slett.addActionListener(k);
        tilbake.addActionListener(k);
        kpan.add(slett);
        kpan.add(tilbake);

        add(liste, BorderLayout.NORTH);
        add(kpan, BorderLayout.SOUTH);

}
     private class Knappelytter implements ActionListener{
         public void actionPerformed(ActionEvent h){
             JButton k = (JButton)h.getSource();
             Bruker b = (Bruker)liste.getSelectedValue();

             if(k.getText().equals("Slett")){
                 int svar = JOptionPane.showConfirmDialog(null, "Er du sikker på du vil slette denne brukeren?");
                 if(svar == JOptionPane.YES_OPTION){
                      o.slettBruker(b);
                     brukere.clear();
                     brukere = o.getElever(o.getBruker().getBruker_id());
                     liste = new JList(brukere.toArray());
                     liste.setModel(model);
                     liste = new JList(brukere.toArray());
                     l = new JScrollPane(liste);
                     JOptionPane.showMessageDialog(null,"Personen er slettet.");
                 }
             }
             else if (k.getText().equals("Tilbake")) {
                    
                    GUIAdminpanel j = new GUIAdminpanel(o);
                    
                    JPanel mainPanel = (JPanel)getParent();
                    System.out.println(mainPanel.getParent().toString());
                    mainPanel.setBounds(0,0,mainPanel.getWidth(),mainPanel.getHeight());
                    mainPanel.removeAll(); // dette is the sub-panel
                    
                    mainPanel.add(j);
                    mainPanel.repaint();
                    mainPanel.revalidate();
                    j.repaint();
                    j.revalidate();
             }
         }

     }

    
     
}
